package grpc

import (
	"context"
	"google.golang.org/grpc"
	paymentv1 "jk-time/webook/api/proto/gen/payment/v1"
	"jk-time/webook/payment/domain"
	"jk-time/webook/payment/service/wechat"
)

type WechatServiceServer struct {
	paymentv1.UnimplementedPaymentSerivceServer
	svc *wechat.NativePaymentService
}

func NewWechatServiceServer(svc *wechat.NativePaymentService) *WechatServiceServer {
	return &WechatServiceServer{svc: svc}
}
func (s *WechatServiceServer) Register(server *grpc.Server) {
	paymentv1.RegisterPaymentSerivceServer(server, s)
}
func (i *WechatServiceServer) NativePrepay(ctx context.Context,
	in *paymentv1.PrepayRequest) (*paymentv1.PrepayResponse, error) {
	codeURL, err := i.svc.Prepay(ctx, domain.Payment{
		Amt: domain.Amount{
			Currency: in.Amt.Currency,
			Total:    in.Amt.Total,
		},
		BizTradeNO:  in.BizTradeNo,
		Description: in.Description,
	})
	if err != nil {
		return nil, err
	}
	return &paymentv1.PrepayResponse{
		CodeUrl: codeURL,
	}, nil
}
func (i *WechatServiceServer) GetPayment(ctx context.Context, in *paymentv1.GetPaymentRequest) (*paymentv1.GetPaymentResponse, error) {
	p, err := i.svc.GetPayment(ctx, in.GetBizTradeNo())
	if err != nil {
		return nil, err
	}
	return &paymentv1.GetPaymentResponse{
		Status: paymentv1.PaymentStatus(p.Status),
	}, nil
}
